<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<script>
    //practice2
    function sum(param1,param2){
        return param1+param2
    }
    //practice3
    function changeNum(num1,num2){
        let temp = num1
        num1 = num2
        num2 = temp
        document.write(`交换过后，第一个值为${num1},第二个值为${num2}<br>`)
        return[num1,num2]
    }
    changeNum(1,2)
    changeNum(...changeNum(1,2))
    //practice4
    function count(){
        let bank_balance = +prompt('请输入银行卡余额')
        let live_cost = +prompt('请输入当月食宿消费')
        let food_cost = +prompt('请输入当月生活消费')
        alert(`您当月的余额是${bank_balance-live_cost-food_cost}`)
    }
    // count()
    //practice5
    function handleData(arr,flag=true){
        //求和
        let sum = 0
            for(let i of arr){
                sum += i
            }
        if(flag){
            return sum
        }else{
            //求平均值
            return sum/arr.length
        }
    }
    document.write(`和为${handleData([1,31,6,7])}<br>`)
    document.write(`平均值为${handleData([1,31,6,7],false)}<br>`)

    //another1
    function some(ele, arr = []){
        let flag = arr.indexOf(ele)
        if(flag == -1){
            return false
        }else{
            return true
        }
    }
    let re = some('荔枝', ['苹果', '香蕉', '橘子', '荔枝', '梨子'])
    console.log(re) // true
    let re1 = some('榴莲', ['苹果', '香蕉', '橘子', '荔枝', '梨子'])
    console.log(re1) // false
    
    //another2
    // 封装函数返回元素的下标  [1, 5, 10, 22, 8, 7]
    // 1. 封装函数 findIndex，传递2个参数 元素、数组
    function findIndex(ele, arr = []) {
     	return arr.indexOf(ele)
    }
    let index1 = findIndex(10, [1, 5, 10, 22, 8, 7])
    console.log(index1) // 2
    let index2 = findIndex(8, [1, 5, 10, 22, 8, 7])
    console.log(index2) // 4
    let index3 = findIndex(88, [1, 5, 10, 22, 8, 7])
    console.log(index3) // -1
</script>
<body>
    
</body>
</html>